﻿using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace RRRSRoguelikeEngine
{
	/// <summary>
	/// Manages the game flow.
	/// </summary>
	class GameManager
	{
		#region variables

		/// <summary>
		/// Time passed 
		/// </summary>
		public Stopwatch Time;

		/// <summary>
		///Game time per dungeon
		/// </summary>
		public Stopwatch GameTime;

		/// <summary>
		/// A list of stopwatches for general use.
		/// </summary>
		public Dictionary<string, Stopwatch> StopWatches = new Dictionary<string, Stopwatch>();

		/// <summary>
		/// A basic screen manager 
		/// </summary>
		public Stack<IScreen> ScreenStack = new Stack<IScreen>();

		/// <summary>
		/// keeps a lost of all texts to be used in game menus outside of a dungeon instance
		/// </summary>
		public Dictionary<string, string> Texts = new Dictionary<string, string>();

		/// <summary>
		/// handles all input
		/// </summary>
		public IInputManager InputManager;

		/// <summary>
		/// handles sound outside of a dungeon instance
		/// </summary>
		public ISoundManager SoundManager;

		#endregion
		#region singleton
		static readonly GameManager instance = new GameManager();

		public static GameManager Instance
		{
			get { return instance; }
		}
		#endregion
		#region methods
		/// <summary>
		/// Initializes the gameScreens.
		/// </summary>
		private void Initialize(ISoundManager soundManager, IInputManager inputManager)
		{
			InitializeManagers(soundManager, inputManager);
			SetupTime();
		}

		static void InitializeManagers(ISoundManager soundManager, IInputManager inputManager)
		{
			Instance.SoundManager = soundManager;
			Instance.InputManager = inputManager;
		}

		void SetupTime()
		{
			Time = new Stopwatch();
			Time.Start();
			GameTime = new Stopwatch();
		}

		/// <summary>
		///Used to design game flow.
		///Splash and menu screens come off the stack first, credit remains until exit, with no screens pushed on top.  
		/// </summary>

		/// <summary>
		/// Entry point. Controls the gameflow.
		/// </summary>
		public void Run()
		{

			//This is the main engine or loop of the game and calls screens one by one to show to the user.
			while (ScreenStack.Count > 0)
				ScreenStack.Pop().Show();
		}

		public void Exit()
		{
			//Exit console
			Environment.Exit(0);
		}

		public static void SetConsoleSize(int width, int height)
		{
			//getting bigger
			if (width >= Console.WindowWidth && height >= Console.WindowHeight)
			{
				Console.SetBufferSize(width, height);
				Console.SetWindowSize(width, height);
			}
			//getting smaller
			else if (width <= Console.WindowWidth && height <= Console.WindowHeight)
			{
				Console.SetWindowSize(width, height);
				Console.SetBufferSize(width, height);
			}
			//width bigger height smaller
			else if (width >= Console.WindowWidth && height <= Console.WindowHeight)
			{
				Console.SetWindowSize(Console.WindowWidth, height);
				Console.SetBufferSize(width, height);
				Console.SetWindowSize(width, height);
			}
			//width smaller height bigger
			else if (width <= Console.WindowWidth && height >= Console.WindowHeight)
			{
				Console.SetWindowSize(width, Console.WindowHeight);
				Console.SetBufferSize(width, height);
				Console.SetWindowSize(width, height);
			}
		}
		#endregion
	}
}